﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dss.Data;

namespace Dss.Methods
{
    public class ProjectTeam
    {
        public ProjectTeam() 
        {
            cecbContext = new DSSEntities();
        }

        DSSEntities cecbContext;
        Team team;
        Team_Member tmem;
        public bool InsertProjectTeam(String name  , List<Employee> list , string memId)
        {
            bool IsInserted = false;
            try
            {
                String reference = getNewProjectTeamReference();
                team = new Team();
                team.Team_Reference = reference;
                team.Team_Name = name;
                team.Team_Leader = memId;
                team.Team_CreatedDate = DateTime.Now;
                team.Team_Status = true;
                cecbContext.AddToTeams(team);
                cecbContext.SaveChanges();

                

                foreach (Employee value in list)
                {
                    tmem = new Team_Member();
                    tmem.Team_Reference = reference;
                    tmem.Team_MemberID = value.EmpReference;
                    tmem.Team_Member_Status = true;
                    cecbContext.AddToTeam_Member(tmem);
                    cecbContext.SaveChanges();
                }
                  
                IsInserted = true;
            }
            catch (Exception)
            {

                throw;
            }


            return IsInserted;
        }

        public String getNewProjectTeamReference()
        {

            var idKey = (from c in cecbContext.Teams
                         orderby c.TeamID descending
                         select c).First();

            String id = idKey.Team_Reference.ToString();
            int sub = int.Parse(id.Substring(4)) + 1;
            String newId = "Team" + sub;
            return newId.ToString();
        }

        public List<Employee> getTeamLeaders()
        {
            String designation = "Leader";
            IQueryable<Employee> Query =
                  from c in cecbContext.Employees
                  where c.EmpDesignation == designation
                  select c;

            List<Employee> SelectedEmpList = Query.ToList();

            return SelectedEmpList;

        }

        public List<Team> getProjectTeams() 
        {
            IQueryable<Team> Query =
                  from c in cecbContext.Teams               
                  select c;

            List<Team> SelectedTeamList = Query.ToList();

            return SelectedTeamList;
        }
       
    }
}
